<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24 0024
 * Time: 下午 15:37
 */
namespace app\admin\controller;

use think\Db;
use \think\Session;

class  deposit extends  Permissions
{
    public function  index()
    {
        $ress = Session::get("admin");//获取登陆时的admin，控制器comment中的admin(126行）
        $list = Db::name('admin')->where('id',$ress)->value('name');
        $lists = Db::name('agent')->where('phone',$list)->find();


     if($this->request->isPost()) {
     	$money = input('money');
     	if(empty($money)){
     		return $this->error('提交失败,请输入金额');
     	}
     	if($money < 500){
     	   return $this->error('金额不能小于500');
     	}
        if($money > 5000){
           return $this->error('金额不能大于5000');
        }
        if($money > $lists['last_money']){
             return $this->error('账户余额不足');
        }
     	 if(empty($lists)){
        	 return $this->error('参数出错，请重新提交');
        }
     	$list_money = $lists['last_money']-$money;
     	$realiy = $money*0.99;
     	$data = array(
     		 	'user_id'=>$lists['id'],
     		 	'money'=>$money,
     		 	'reality_money'=>$realiy,
     		 	'status'=>1,
     		 	'add_time'=>time(),
     		 	'last_money'=>$list_money
     	);
     	$lis = Db::name('cash')->insert($data);

     	if($lis){
     		$lid = Db::name('agent')->where('id',$lists['id'])->setDec('last_money',$money);
     		if($lid){
                $config  =  read_static_cache("cash");
                if( $config ==0 ){
                    return $this->success('提交成功','admin/cash/index');
                }else{
                    $result = Db::name('cash')->where('user_id',$lists['id'])->order('id desc')->limit(1)->find();
                    $id = $result['id'];
                    $url = "http://csmj.qiligame.com/admin/cash/through/id/$id";
                     header("Location:".$url);
                  
                }

     			
     		}
     		
     	}


    }
    $this->assign('lists',$lists);

    return $this->fetch();
    }
}